<?php
/**
 * wiki详情也下，wiki列表推荐
 * Class MiddleListAction
 */

use Biz\service\wiki\WikiMiddle;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzClient as tHhzClient;

class AiWikiListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;
    const IS_LOG_EXPOSE = 1;

    /**
     * 获取详情页推荐wiki列表
     * @return array|array[]
     */
    public function main()
    {
        $wiki_id = (int)$this->getRequest()->getPost('wiki_id', 0);
        try {
            if ($wiki_id <= 0) {
                throw new \Exception('wiki参数错误');
            }
            $uid = $this->uid;
            $data = WikiMiddle::getAiWikiList($wiki_id, $uid);
            //埋点
            foreach ($data as $key => $v) {
                if($key == $wiki_id){
                    unset($data[$key]);
                }else{
                    $extend[basicStatSign::EXTEND_KEY_OBJ_ID] = $key;
                    $extend[basicStatSign::EXTEND_KEY_OBJ_TYPE] = BlockType::TYPE_WIKI;
                    $data[$key]['statSign'] = basicStatSign::genStatSign(basicStatSign::MODULE_WIKI_DETAIL_LIST, $extend,
                        ['brand_id' => $v['brand_id'], 'act_from' => 'wiki_detail', 'wiki_id' => $v['id']]);
                }
            }


            $list['list'] = $data ? array_values($data) : [];
            if(tHhzClient::$client_type == tHhzClient::APPTYPE_IOS || tHhzClient::$client_type == tHhzClient::APPTYPE_ANDROID){
                $list['list'] = \Doraemon\tools\StatSign::versionCompatible($list['list'], \Doraemon\tools\StatSign::WIKI_UP_GRADE_VERSION_522, \Doraemon\tools\BlockType::TYPE_WIKI, 'wiki' );
            }else{
                tHhzClient::$appVersion = '1';
                $list['list'] = \Doraemon\tools\StatSign::versionCompatible($list['list'], '0', \Doraemon\tools\BlockType::TYPE_WIKI, 'wiki' );
            }
        } catch (\Exception $e) {
            return $this->setSuccess([], self::API_CODE_ERROR, $e->getMessage());
        }
        $this->setSuccess($list);
    }
}